﻿using LbangCMS.Areas.Admin.Models;
using LbangCMS.EFModels;
using LbangCMS.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Areas.Admin.Controllers
{
    [Authorize]
    public class VideoController : Controller
    {
        // GET: Admin/Content
        public ActionResult List(int categoryId=0,int pageIndex=1,int pageSize=12)
        {
            int totalPage;
            var contents = GetList(categoryId, pageIndex, pageSize,out totalPage);
            var data = new List<VideoModel>();
            var model = new VideoListModel();
            if(contents!=null&contents.Any())
            {
                foreach (var item in contents)
                {
                    var m = new VideoModel();
                    m.ID = item.ID;
                    m.CategoryId = item.CategoryId;
                    m.Name = item.Name;
                    m.ImageSrc = item.ImageSrc;
                    m.CreateTime = item.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    data.Add(m);
                }
                model.VideoList = data;
                model.PageIndex = pageIndex;
                model.TotalPage = totalPage;


            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListPartial", model);
            }
            else
            {
                return View(model);
            }
        }

        public ActionResult Edit(int id=0)
        {
            var videoCategories = GetCategoryList();
            if (videoCategories != null && videoCategories.Any())
            {
                var categoryModel = new List<VideoCategoryModel>();
                foreach (var item in videoCategories)
                {
                    categoryModel.Add(new VideoCategoryModel { ID = item.ID, Name = item.Name });
                }
                ViewBag.Categories = categoryModel;
            }
            if (id>0)
            {
                var content = GetVideo(id);
                if(content!=null)
                {
                    var model = new VideoModel();
                    model.ID = content.ID;
                    model.CategoryId = content.CategoryId;
                    model.ImageSrc = content.ImageSrc;
                  
                    model.Body = content.Body;
                    return View(model);
                }
            }
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(VideoModel model)
        {
            if(ModelState.IsValid)
            {
                if(model.ID>0)
                {
                    UpdateVideo(model.ID, model.CategoryId, model.Name, model.ImageSrc,model.Body);
                }
                else
                {
                    AddVideo(model.CategoryId, model.Name, model.ImageSrc, model.Body);
                }
                return Json(new { success = true });
            }
            else
            {
                return Json(new { success = false, errorMsg = ModelState.ToHtmlMessage() });
            }
            
        }


        public ActionResult Remove(int id)
        {
            RemoveVideo(id);
            return Json(new { success = true });
        }

        private List<VideoCategory> GetCategoryList()
        {
            using (var db = new Database())
            {
                return db.VideoCategories.OrderBy(o => o.SortIndex).ToList();
            }
        }

        private List<Video> GetList(int categoryId,int pageIndex,int pageSize,out int totalPage)
        {
            using (var db = new Database())
            {
                if (categoryId > 0)
                {
                    var count = db.Videos.Where(o => o.CategoryId == categoryId).Count();
                    totalPage = (int)Math.Ceiling(count / (pageSize * 1M));
                    return db.Videos.Where(o => o.CategoryId == categoryId).OrderByDescending(o => o.ID).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    var count = db.Videos.Count();
                    totalPage = (int)Math.Ceiling(count / (pageSize * 1M));
                    return db.Videos.OrderByDescending(o => o.ID).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
            }
        }

        private void RemoveVideo(int id)
        {
            using (var db = new Database())
            {
                var content = db.Videos.Where(o => o.ID == id).FirstOrDefault();
                if(content!=null)
                {
                    db.Videos.Remove(content);
                    db.SaveChanges();
                }
            }
        }

        private Video GetVideo(int id)
        {
            using (var db = new Database())
            {
                return db.Videos.Where(o => o.ID == id).FirstOrDefault();
            }
        }

        public void AddVideo(int categoryId, string name, string imageSrc, string body)
        {
            using (var db = new Database())
            {
                var video = new Video();

                video.Name = name;
                video.CategoryId = categoryId;
                video.ImageSrc = imageSrc;
                video.Body = body;
                video.CreateTime = DateTime.Now;
                db.Videos.Add(video);
                db.SaveChanges();

            }
        }

        private void UpdateVideo(int id, int categoryId, string name, string imageSrc, string body)
        {
            using (var db = new Database())
            {
                var video = db.Videos.Where(o => o.ID == id).FirstOrDefault();
                if (video != null)
                {
                    video.Name = name;
                    video.CategoryId = categoryId;
                    video.ImageSrc = imageSrc;
                    video.Body = body;
                    db.SaveChanges();
                }
            }
        }

    }
}